package com.ggh.jinjilive.ui.mall.shop;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityShopBinding;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.shop.ShopMessageMobile;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mall.shop.adapter.ShopAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.views.DividerItemDecoration;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * 店铺
 */
@Route(path = ARouterUtils.ShopActivity)
public class ShopActivity extends BaseActivity<ActivityShopBinding> implements View.OnClickListener, OnRefreshListener, OnLoadMoreListener {
    //VM
    private ShopViewModel shopViewModel;
    //店铺ID
    private int ShopId;
    //适配器
    private ShopAdapter shopAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        shopViewModel = new ViewModelProvider(this,viewModelFactory).get(ShopViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_shop;
    }

    @Override
    public void initParam() {
        super.initParam();
        ShopId = getIntent().getIntExtra("ShopId",0);

    }

    @Override
    public void initView() {
        super.initView();
         loadService = LoadSir.getDefault().register(binding.refreshLayout1, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                //店铺商品
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        //全部
        binding.tvAll.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
        binding.tvSalesofmorethan.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        ////是否启用上拉加载功能
        binding.refreshLayout.setEnableLoadMore(false);
        //是否启用下拉刷新功能
        binding.refreshLayout1.setEnableRefresh(true);
        binding.refreshLayout1.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initData() {
        super.initData();
        //获取店铺信息
        shopViewModel.storeInfo(ShopId);
        //店铺商品
        shopViewModel.isRefresh = true;
        shopViewModel.productList(ShopId);
        //商品数据
        shopAdapter = new ShopAdapter();
        binding.rv.addItemDecoration(new DividerItemDecoration(mContext));
        binding.rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.rv.setAdapter(shopAdapter);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout1.setOnLoadMoreListener(this);
        //全部商品
        binding.tvAll.setOnClickListener(this);
        //推荐
        binding.tvRecommend.setOnClickListener(this);
        //分类
        binding.tvClassify.setOnClickListener(this);
        //特价
        binding.tvSpecialOffer.setOnClickListener(this);
        //促销
        binding.tvPromotion.setOnClickListener(this);
        //销量多
        binding.tvSalesofmorethan.setOnClickListener(this);
        //新发布
        binding.tvNewlyRelease.setOnClickListener(this);
        //关注店铺
        binding.tvFollow.setOnClickListener(this);
        //获取店铺信息
        shopViewModel.mShopMessageMobile.observe(this, new Observer<ShopMessageMobile>() {
            @Override
            public void onChanged(ShopMessageMobile shopMessageMobile) {
                if(null != shopMessageMobile){
                    setWhiteActionBarStyle(shopMessageMobile.getName(),true,true);
                    //名称
                    binding.tvName.setText(shopMessageMobile.getName());
                    //是否运营
                    binding.tvStatusName.setText(shopMessageMobile.getStatus_name());
                    //是否关注

                    LogUtils.d("店铺是否关注" + shopMessageMobile.getIs_collect() );
                    if(shopMessageMobile.getIs_collect() == 0){
                        shopViewModel.attention.setValue(false);
                    }else {
                        shopViewModel.attention.setValue(true);
                    }
                    //
                    binding.arb.setRating(4);

                    //开门时间
                    binding.tvOpenTime.setText(shopMessageMobile.getStart_time() +"  ~  "+ shopMessageMobile.getEnd_time());
                    //关注人数
                    binding.tvAttention.setText(shopMessageMobile.getCollect_store() +"人关注");
                    //人逛过
                    binding.tvStroll.setText(shopMessageMobile.getToday_vistor() +"人逛过");
                    //图片一
                    Glide.with(binding.productImg1).load(shopMessageMobile.getImg()).into(binding.productImg1);
                    for (int i = 0;i < shopMessageMobile.getString().size();i++){
                        if(i == 0){
                            Glide.with(binding.productImg2).load(shopMessageMobile.getString().get(i)).into(binding.productImg2);
                        }else if(i == 1){
                            Glide.with(binding.productImg3).load(shopMessageMobile.getString().get(i)).into(binding.productImg3);
                        }
                    }
                    //地址
                    binding.tvSAddress.setText(shopMessageMobile.getS_address() +" " + shopMessageMobile.getAddress());
                    //公告
                    binding.announcement.setText("公告:" + shopMessageMobile.getContent());
                }else {
                    setWhiteActionBarStyle("店铺",true,true);
                }
            }
        });
        //店铺商品
        shopViewModel.mMallGoodsList.observe(this, new Observer<List<MallGoods>>() {
            @Override
            public void onChanged(List<MallGoods> mallGoods) {
                binding.refreshLayout.finishRefresh();
                if(shopViewModel.isRefresh){
                    if(null != mallGoods && mallGoods.size() > 0){
                        loadService.showSuccess();
                        shopAdapter.setAdd(mallGoods);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(mallGoods.size() > 0){
                        shopAdapter.ADD(mallGoods);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
        //店铺是否关注
        shopViewModel.attention.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                LogUtils.d("店铺是否关注" + aBoolean);
                //是否关注
                if(aBoolean){
                    binding.tvFollow.setText("已关注");
                }else {
                    binding.tvFollow.setText("+ 关注");
                }
            }
        });
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.tv_all://全部商品
                initTabs();
                binding.tvAll.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type1.set(0);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_recommend://推荐
                initTabs();
                binding.tvRecommend.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type1.set(1);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_classify://分类
                initTabs();
                binding.tvClassify.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type1.set(2);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_specialOffer://特价
                initTabs();
                binding.tvSpecialOffer.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type1.set(3);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_promotion://促销
                initTabs();
                binding.tvPromotion.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type1.set(4);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_Salesofmorethan://销量多
                initSubTabs();
                binding.tvSalesofmorethan.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type2.set(0);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_newlyRelease://新发布
                initSubTabs();
                binding.tvNewlyRelease.setBackgroundColor(getResources().getColor(R.color.md_blue_A700));
                shopViewModel.type2.set(1);
                shopViewModel.isRefresh = true;
                shopViewModel.productList(ShopId);
                break;
            case R.id.tv_follow://关注店铺
                if(shopViewModel.attention.getValue()){
                    shopViewModel.delCollectStore(ShopId +"");
                }else {
                    shopViewModel.addCollectStore(ShopId);
                }
                break;


        }
    }

    private void initTabs(){
        binding.tvAll.setBackgroundColor(0);
        binding.tvRecommend.setBackgroundColor(0);
        binding.tvClassify.setBackgroundColor(0);
        binding.tvSpecialOffer.setBackgroundColor(0);
        binding.tvPromotion.setBackgroundColor(0);
    }

    private void initSubTabs(){
        binding.tvSalesofmorethan.setBackgroundColor(0);
        binding.tvNewlyRelease.setBackgroundColor(0);
    }


    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        shopViewModel.isRefresh = true;
        shopViewModel.productList(ShopId);
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        //获取订单信息
        shopViewModel.isRefresh = true;
        shopViewModel.productList(ShopId);
    }
}
